import { reqTenantList, reqProductModelPage, reqTerminalTask } from "@api/api.js";

export default {
    data() {
        return {
            enterpriseList: [],
            classMarkList: [],
            sysInstanceIndex: 1,
            sysInstanceTotalPages: 10,
            loading: false,
            flag: true,
            protocolList: [{
                    id: 1,
                    name: "电信物模型标准规范",
                },
                {
                    id: 2,
                    name: "移动铠湾规范",
                },
                {
                    id: 3,
                    name: "移动安消规范",
                }, {
                    id: 6,
                    name: "声光规范",
                },
                {
                    id: 7,
                    name: "珠海慧芯",
                },
            ],
            // 导出筛选条件
            exportList: {
                online: "",
                productId: "",
                imei: "",
                minBattery: "",
                maxBattery: "",
                tenantId: "",
                tamper: "",
                isAlarm: "",
                classMark: "",
                protocol: "",
            },
        };
    },
    mounted() {},
    methods: {
        // 搜索
        async searchClick1() {
            this.page = 1;
            this.limit = 10;
            this.classMark2 = this.searchForm.classMark; //批量下发
            this.protocol2 = this.searchForm.protocol; //批量下发
            this.showPopover = false;
            await this.getDataList();
            this.clearSelections();
        },
        // 高级搜索重置
        async resetClick() {
            this.searchForm.online = "";
            this.searchForm.productId = "";
            this.searchForm.manufacturerId = "";
            this.searchForm.minBattery = "";
            this.searchForm.maxBattery = "";
            this.searchForm.isAlarm = "";
            this.searchForm.tenantId = "";
            this.searchForm.protocol = "";
            this.searchForm.classMark = "";
            this.searchForm.tamper = "";
            this.classMark2 = ""; //批量下发
            this.protocol2 = ""; //批量下发
            this.page = 1;
            this.limit = 10;
            await this.getDataList();
            this.clearSelections();
        },
        // 租户
        async loadMore() {
            if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10))
                return;
            this.sysInstanceIndex++;
            this.loading = true;
            // 获取列表数据的方法
            await this.getEnterpriseList();
            this.loading = false;
        },
        // 下拉请求列表
        enterprise(val) {
            if (val && this.flag) {
                this.getEnterpriseList();
                this.flag = false; //只执行一次
            }
        },
        onClear() {
            this.searchForm.tenantId = ''; //保留输入的值
            this.tenantName = '';
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.enterpriseList = [];
            this.getEnterpriseList();
        },
        // 远程搜索租户
        async remoteMethodEnterprise(query) {
            // console.log(query, "query");
            this.searchForm.tenantId = query; //保留输入的值
            this.tenantName = query;
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.enterpriseList = [];
            this.getEnterpriseList();
        },
        // 获取租户列表
        async getEnterpriseList() {
            var res = await reqTenantList(this.sysInstanceIndex, 10, {
                name: this.tenantName || "",
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages = res.data.total;
            this.enterpriseList = [...this.enterpriseList, ...res.data.rows];
            // res.data.rows.forEach((e) => {
            //   this.enterpriseList.push({ id: e.id, name: e.name });
            // });
        },
        // 获取产品型号
        async ProductModelPage(e) {
            this.classMarkList = [];
            this.searchForm.classMark = "";
            if (!e) return;
            let res = await reqProductModelPage(1, 30, {
                productId: this.searchForm.productId,
            });
            // console.log(res);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.classMarkList = res.data.rows;
        },
        // 导出  设备筛选有几个字段，接口就要加几个字段
        async onExports() {
            const result = await this.$confirm(
                "此操作将导出设备信息, 是否继续?",
                "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            ).catch(() => {});
            if (result !== "confirm") return;
            const res = await reqTerminalTask(this.exportList);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.$modal.notifySuccess("导出成功，请在任务管理中查看");
            this.$router.push({ name: "exportTask" });
        },
    },
};